<?php

require_once 'common.php';
require_once 'ResultData.php';
require_once 'config.php';

session_start();


if (isGet()) {
    echo "不得以GET访问";
    exit;
}


$name = trim($_REQUEST['name']);
$psd = trim($_REQUEST['password']);
$auto = trim($_REQUEST['auto']);


$result = new ResultData();
if ($name == "") {
    $result->code = 404;
    $result->msg = "name不能为空";
    exit(json_encode($result));
}
if ($psd == "") {
    $result->code = 404;
    $result->msg = "password不能为空";
    exit(json_encode($result));
}


$con = mysqli_connect($servername, $username, $password, $database, $port);

if (!$con) {
    $result->code = 404;
    $result->msg = "数据库连接不上";
    exit(json_encode($result));
}

$sql = "SELECT count(*) num FROM t_login WHERE name= '{$name}' AND password = '{$psd}'";


$record = $con->query($sql);

if ($record->num_rows > 0) {
    while ($row = $record->fetch_assoc()) {
        $result->msg = "登录成功";
        $_SESSION['auto'] = $auto;
        if ($row["num"] <= 0) {
            $result->code = 404;
            $result->msg = "用户名或密码不正确";
        } else {
            if ($auto) {
                setcookie("PHPSESSID", session_id(), time() + 3600 * 24 * 30, "/");
                $_SESSION['username'] = $name;
                $_SESSION['password'] = $psd;
            }
        }
    }
}

mysqli_close($con);


exit(json_encode($result));
